import type { RouteRecordRaw } from 'vue-router'
import { RouteMenuType } from '../types'
import { MenuType } from '@/api/auth/menu/types'

// 引入 views 文件夹下所有 vue 文件
const modules = import.meta.glob('@/views/**/*.vue')

export const useRouteStore = defineStore(STORE_ROUTE, {
    state: (): RouteMenuType => {
        return {
            viewRoutes: [],
            authRoutes: [],
            authButtons: [],
        }
    },
    getters: {},
    actions: {
        // 获得菜单权限
        async getAuthRoutes() {
            const result = await getAllMenus()
            if (!result.isSuccess) return null
            this.authRoutes = this._getChildren(result.data)
            const dynamicRoutes = this.authRoutes.filter((item: RouteRecordRaw) => item.meta?.parentName === null)
            this.viewRoutes = [...constRoutes, ...dynamicRoutes, ...anyRoutes].filter((item: RouteRecordRaw) => item.meta?.isHidden !== true)
        },
        _getChildren(arr: MenuType[]): RouteRecordRaw[] {
            const dynamicRoutes: RouteRecordRaw[] = []
            arr.forEach((item) => {
                let route: RouteRecordRaw = {
                    path: item.path,
                    name: item.name,
                    redirect: item.redirect ?? '',
                    meta: {
                        title: item.title,
                        icon: item.icon,
                        isHidden: item.isHidden,
                        isKeepalive: item.isKeepalive,
                        isAffis: item.isAffis,
                        parentName: item.parentName,
                        componentPath: item.componentPath,
                    },
                }

                if (item.children?.length === 0) {
                    dynamicRoutes.push(route)
                } else {
                    route.children = this._getChildren(item.children)
                    dynamicRoutes.push(route)
                }
            })
            return dynamicRoutes
        },
        // 初始化权限路由
        async initAuthRoute() {
            // 存在权限路由，则添加
            if (this.authRoutes.length !== 0) {
                this.authRoutes.forEach((route: RouteRecordRaw) => {
                    if (route.meta?.componentPath === 'Layout') {
                        route.component = () => import('@/layout/Layout.vue')
                    } else {
                        route.component = route.meta?.componentPath ? modules[route.meta?.componentPath] : undefined
                    }
                    const parentName = route.meta?.parentName
                    parentName ? router.addRoute(parentName, route) : router.addRoute(route)
                })
            }
            // 添加任意路由
            anyRoutes.forEach((route: RouteRecordRaw) => {
                router.addRoute(route)
            })
        },
        // 获得按钮权限
        async getAuthButtons() {
            // const result = await getMenus()
            // if (!result.isSuccess) return
            // this.authButtons = result.data
        },
    },
    persist: true,
})
